package com.shuimin.service;

import com.shuimin.service.model.sys.Shop;
import com.shuimin.service.service.sys.ShopService;
import com.shuimin.service.service.sys.UserService;
import pond.common.S;
import pond.common.STRING;
import pond.db.Record;
import pond.web.EndToEndException;
import pond.web.Render;
import pond.web.Router;

import static com.shuimin.service.util.RespUtil.error;
import static com.shuimin.service.util.RespUtil.success;


/**
 * Created by sky on 15/12/24.
 */
public class SHOP extends Router {
    {
        get("/", (req, resp) -> {
            String name = req.param("name");
            if (STRING.isBlank(name)) resp.render(Render.json(ShopService.all()));
            else
                resp.render(Render.json(ShopService.getByName(name)));
        });
        get("/all/normal", (req, resp) -> {
            String name = req.param("name");
            if (STRING.isBlank(name)) resp.render(Render.json(ShopService.allForNormal()));
            else
                resp.render(Render.json(ShopService.getByName(name)));
        });

        get("/:id", (req, resp) -> {
            String id = req.param("id");
            resp.render(Render.json(ShopService.get(id)));
        });

        post("/", (req, resp) -> {
            App.DB.post(t -> {
                ShopService.addShop((Shop) Record.newEntity(Shop.class).merge(req.toMap()), t);
            });
            success(resp, "");
        });

        put("/sort", (req, resp) -> {

            String _shops = req.param("sorted_shops");
            String _disable_shops = req.param("disable_shops");
            String[] shops = _shops.split(";");
            String[] disable_shops = _disable_shops.split(";");
            if (shops.length <= 0) {
                error(resp, 400, 4000101, "门店不能为空");
                return;
            }
            if (disable_shops.length < 0) {
                error(resp, 400, 4000101);
                return;
            }
            App.DB.post(t -> {
                for (int i = 0; i < shops.length; i++) {
                    String shop_id = shops[i];
                    Shop shop = ShopService.get(shop_id);
                    if (shop == null) continue;
                    shop.set("seq", i + 1);
                    ShopService.updShop(shop, t);
                }

                for (int i = 0; i < disable_shops.length; i++) {
                    String shop_id = disable_shops[i];
                    Shop shop = ShopService.get(shop_id);
                    if (shop == null) continue;
                    shop.set("seq", -i );
                    ShopService.updShop(shop, t);
                }

            });
            resp.send(200);
        });

        put("/:id", (req, resp) -> {
            String id = req.param("id");
            App.DB.post(t -> {
                Shop shop = ShopService.get(id);
                S._assert(shop);
                shop.merge(req.toMap());
                ShopService.updShop(shop, t);
            });
            resp.send(200, id);
        });

        del("/:shop_id", (req, resp) -> {
            String id = req.param("shop_id");
            S._assert(id);
            App.DB.post(t -> {
                ShopService.delShop(ShopService.get(id), t);
            });
            resp.send(200, id);
        });

        get("/:shop_id/users", (req, resp) -> {
            String shop_id = req.param("shop_id");
            if (STRING.isBlank(shop_id)) {
                error(resp, 400, 4040201);
                return;
            }
            resp.render(Render.json(UserService.getByShop(shop_id)));
        });


    }
}
